﻿Option Strict On
Option Explicit On


Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web


Imports System.Collections.Specialized
Imports System.Text
Imports System.IO


Namespace Logs

    ''' <summary>
    ''' Log da instanzare etc etc
    ''' </summary>
    ''' <remarks></remarks>
    Public Class Log

        Private _defaultSubject As String
        Private _logFile As String
        Private _smtp As String
        Private _mailTo As String
        Private _mailFrom As String



        ''' <summary>
        ''' 
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property MailTo() As String
            Get
                Return _mailTo
            End Get
            Set(ByVal value As String)
                _mailTo = value
            End Set
        End Property

        ''' <summary>
        ''' 
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property MailFrom() As String
            Get
                Return _mailFrom
            End Get
            Set(ByVal value As String)
                _mailFrom = value
            End Set
        End Property

        ''' <summary>
        ''' 
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property Smtp() As String
            Get
                Return _smtp
            End Get
            Set(ByVal value As String)
                _smtp = value
            End Set
        End Property


        ''' <summary>
        ''' 
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property LogFile() As String
            Get
                Return _logFile
            End Get
            Set(ByVal value As String)
                _logFile = value
            End Set
        End Property

        ''' <summary>
        ''' 
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property DefaultSubject() As String
            Get
                Return _defaultSubject
            End Get
            Set(ByVal value As String)
                _defaultSubject = value
            End Set
        End Property

        ''' <summary>
        ''' scrivi log su file
        ''' </summary>
        ''' <param name="Message">corpo del mess da loggare</param>
        ''' <param name="SendMail">manda una mail con il log</param>
        ''' <remarks></remarks>
        Public Sub WriteLog(ByVal Message As String, ByVal SendMail As Boolean, ByVal Subject As String)

            Dim path As String = System.IO.Path.GetDirectoryName(Me.LogFile)
            Dim ext As String = System.IO.Path.GetExtension(Me.LogFile)
            Dim fileName As String = System.IO.Path.GetFileNameWithoutExtension(Me.LogFile)

            fileName = fileName & "_" & Format(Date.Now, "yyyy-MM-dd")
            Dim logFile As String = System.IO.Path.Combine(path, fileName) + ext

            Dim SW As StreamWriter
            SW = File.AppendText(logFile)
            SW.WriteLine(Date.Now.ToShortTimeString & vbCrLf & Message)
            SW.Close()

            If SendMail Then
                Dim oMail As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient(Me.Smtp)
                oMail.Send(Me.MailFrom, Me.MailTo, _
                    IIf(Subject = String.Empty, Me.DefaultSubject, Subject).ToString _
                                , Message)
            End If

        End Sub

        ''' <summary>
        ''' scrivi log senza mail
        ''' </summary>
        ''' <param name="Message"></param>
        ''' <remarks></remarks>
        Public Sub WriteLog(ByVal Message As String)
            Me.WriteLog(Message, False, String.Empty)
        End Sub

        ''' <summary>
        ''' scrivi log con subj default
        ''' </summary>
        ''' <param name="Message"></param>
        ''' <param name="SendMail"></param>
        ''' <remarks></remarks>
        Public Sub WriteLog(ByVal Message As String, ByVal SendMail As Boolean)
            WriteLog(Message, SendMail, String.Empty)
        End Sub

    End Class



    ''' <summary>
    ''' log class
    ''' </summary>
    ''' <remarks></remarks>
    Public Class SimpleLog
        ''' <summary>
        ''' 
        ''' </summary>
        ''' <param name="context"></param>
        ''' <param name="errorMessage"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function getErrorWithPageRequestInformation(ByVal context As HttpContext, ByVal errorMessage As String) As String
            Dim message As StringBuilder = New StringBuilder()


            message.Append("******************************************************************" + Environment.NewLine)
            ' ora
            message.Append("- Data: " + DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString() + Environment.NewLine)
            ' url della pagina chiamata
            message.Append("- URL: " + context.Request.Url.AbsoluteUri + Environment.NewLine)
            ' indirizzo ip del server
            message.Append("- REMOTE_ADDR: " + context.Request.ServerVariables("REMOTE_ADDR") + Environment.NewLine)


            message.Append(Environment.NewLine)
            message.Append("*************" + Environment.NewLine)
            message.Append("**  ERROR  **" + Environment.NewLine)
            message.Append("*************" + Environment.NewLine)

            message.Append(errorMessage + Environment.NewLine)

            message.Append(Environment.NewLine)
            message.Append("***********" + Environment.NewLine)
            message.Append("**  GET  **" + Environment.NewLine)
            message.Append("***********" + Environment.NewLine)

            ' inserisco tutti gli elementi in get
            Dim getData As NameValueCollection = context.Request.QueryString
            Dim keys() As String = getData.AllKeys
            Dim i As Integer = 0
            For i = 0 To keys.Length - 1 Step i + 1
                If Not keys(i).StartsWith("_") Then
                    message.Append(keys(i) + ": " + getData(keys(i)) + Environment.NewLine)
                End If
            Next

            message.Append(Environment.NewLine)
            message.Append("************" + Environment.NewLine)
            message.Append("**  POST  **" + Environment.NewLine)
            message.Append("************" + Environment.NewLine)

            ' tutti gli elementi postati con la form
            Dim postData As NameValueCollection = context.Request.Form
            keys = postData.AllKeys

            For i = 0 To keys.Length - 1 Step i + 1
                If Not keys(i).StartsWith("_") Then
                    message.Append(keys(i) + ": " + postData(keys(i)) + Environment.NewLine)
                End If
            Next

            message.Append(Environment.NewLine)
            message.Append("************************" + Environment.NewLine)
            message.Append("**  SERVER VARIABLES  **" + Environment.NewLine)
            message.Append("************************" + Environment.NewLine)

            ' tutte le server variables
            Dim serverVariables As NameValueCollection = context.Request.ServerVariables
            keys = serverVariables.AllKeys

            For i = 0 To keys.Length - 1 Step i + 1
                ' elimino i filed introdotti dal framework quli viewstate ...
                If Not keys(i).StartsWith("_") Then
                    message.Append(keys(i) + ": " + serverVariables(keys(i)) + Environment.NewLine)
                End If
            Next

            Return message.ToString()
        End Function



        ''' <summary>
        ''' 
        ''' </summary>
        ''' <param name="context"></param>
        ''' <param name="errorMessage"></param>
        ''' <param name="NewLineCustomTerminator"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function getErrorWithPageRequestInformation(ByVal context As HttpContext, ByVal errorMessage As String, ByVal NewLineCustomTerminator As String) As String
            Dim message As StringBuilder = New StringBuilder()

            message.Append("******************************************************************" + NewLineCustomTerminator)
            ' ora
            message.Append("- Data: " + DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString() + NewLineCustomTerminator)
            ' url della pagina chiamata
            message.Append("- URL: " + context.Request.Url.AbsoluteUri + NewLineCustomTerminator)
            ' indirizzo ip del server
            message.Append("- REMOTE_ADDR: " + context.Request.ServerVariables("REMOTE_ADDR") + NewLineCustomTerminator)


            message.Append(NewLineCustomTerminator)
            message.Append("*************" + NewLineCustomTerminator)
            message.Append("**  ERROR  **" + NewLineCustomTerminator)
            message.Append("*************" + NewLineCustomTerminator)

            message.Append(errorMessage + NewLineCustomTerminator)

            message.Append(NewLineCustomTerminator)
            message.Append("***********" + NewLineCustomTerminator)
            message.Append("**  GET  **" + NewLineCustomTerminator)
            message.Append("***********" + NewLineCustomTerminator)

            ' inserisco tutti gli elementi in get
            Dim getData As NameValueCollection = context.Request.QueryString
            Dim keys() As String = getData.AllKeys
            Dim i As Integer = 0
            For i = 0 To keys.Length - 1 Step i + 1
                If Not keys(i).StartsWith("_") Then
                    message.Append(keys(i) + ": " + getData(keys(i)) + NewLineCustomTerminator)
                End If
            Next

            message.Append(NewLineCustomTerminator)
            message.Append("************" + NewLineCustomTerminator)
            message.Append("**  POST  **" + NewLineCustomTerminator)
            message.Append("************" + NewLineCustomTerminator)

            ' tutti gli elementi postati con la form
            Dim postData As NameValueCollection = context.Request.Form
            keys = postData.AllKeys

            For i = 0 To keys.Length - 1 Step i + 1
                If Not keys(i).StartsWith("_") Then
                    message.Append(keys(i) + ": " + postData(keys(i)) + NewLineCustomTerminator)
                End If
            Next

            message.Append(NewLineCustomTerminator)
            message.Append("************************" + NewLineCustomTerminator)
            message.Append("**  SERVER VARIABLES  **" + NewLineCustomTerminator)
            message.Append("************************" + NewLineCustomTerminator)

            ' tutte le server variables
            Dim serverVariables As NameValueCollection = context.Request.ServerVariables
            keys = serverVariables.AllKeys

            For i = 0 To keys.Length - 1 Step i + 1
                ' elimino i filed introdotti dal framework quli viewstate ...
                If Not keys(i).StartsWith("_") Then
                    message.Append(keys(i) + ": " + serverVariables(keys(i)) + NewLineCustomTerminator)
                End If
            Next

            Return message.ToString()
        End Function


        ''' <summary>
        ''' 
        ''' </summary>
        ''' <param name="pathFileName"></param>
        ''' <param name="message"></param>
        ''' <remarks></remarks>
        Public Shared Sub WriteError(ByVal pathFileName As String, ByVal message As String)
            Dim path As String = System.IO.Path.GetDirectoryName(pathFileName)
            Dim ext As String = System.IO.Path.GetExtension(pathFileName)
            Dim fileName As String = System.IO.Path.GetFileNameWithoutExtension(pathFileName)
            fileName = fileName + "_" + DateTime.Today.Day.ToString()
            fileName &= "_" + DateTime.Today.Month.ToString()
            fileName &= "_" + DateTime.Today.Year.ToString()

            Dim logFile As String = System.IO.Path.Combine(path, fileName) + ext

            Dim SW As StreamWriter
            SW = File.AppendText(logFile)
            SW.WriteLine(message)
            SW.Close()

        End Sub

        ''' <summary>
        ''' manda una mail semplice
        ''' </summary>
        ''' <param name="FromContact"></param>
        ''' <param name="ToContact"></param>
        ''' <param name="Subject"></param>
        ''' <param name="Body"></param>
        ''' <param name="SmtpServer"></param>
        ''' <remarks></remarks>
        Public Shared Sub SendMailMessage(ByVal FromContact As String, ByVal ToContact As String, ByVal Subject As String, ByVal Body As String, ByVal SmtpServer As String)
            Try

                Dim SmtpClient As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient(SmtpServer)

                SmtpClient.Send(FromContact, ToContact, Subject, Body)

            Catch 'ex As Exception

            End Try

        End Sub

        'Public Shared Sub SendMailMessage(ByVal from As String, ByVal sTo As String, ByVal subject As String, ByVal body As String, ByVal smtpServer As String, ByVal smtpUser As String, ByVal smtpPwd As String)
        '    Try
        '        'cponz 29122006
        '        Dim oMail As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient(smtpServer)
        '        oMail.Send(from, sTo, subject, body)

        '    Catch ex As Exception

        '    End Try

        'End Sub


    End Class

End Namespace


